define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");

	var dingTalkNav = require('dd_config');

	function getRightFun() {
		document.location.href = "../../src/newOutstock/newOutstockOrder.html?dd_nav_bgcolor=FFFF943E";
	}

	var muiTips = require("muiTips"); //封装的弹框
	var keyWordText = ""; //关键字搜索

	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityIdList = authorityInit.getJurisdictionId(authorityData); //权限一级判断
	var authorityWH_OUTPUT = authorityInit.getJurisdiction(authorityData, sourceType.WH_OUTPUT); //库存入库权限判断
	if(authorityIdList.indexOf(sourceType.INFO_GOODS) === -1) {
		$('.mui-content').addClass('initContentTips');
		$('.mui-content > .tipsConInit').html("您没有权限查看库存入库记录资料哦!");
		$("ul.pop-tab > li").addClass('mui-disabled');
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(false, "新增", "default");
	} else {
		if(authorityWH_OUTPUT.indexOf(2) === -1) {
			$("header > a").remove();
			//钉钉右边按钮操作
			dingTalkNav.RightNavBar(false, "新增", "default");
		} else {
			$("#addOrder").show();
			//钉钉右边按钮操作
			dingTalkNav.RightNavBar(true, "新增", getRightFun);
		}
		if(authorityWH_OUTPUT.indexOf(1) === -1) {
			$('.mui-content > .tipsConInit').html("您没有权限查看库存入库记录资料哦!");
			$('.mui-content').addClass("initContentTips");
			$("ul.pop-tab > li").addClass('mui-disabled');
		} else {

			$(function() {
				var getSearchItem = require("getSearchItem"); //调用封装搜索
				getSearchItem.getSearchItem("#chioce-search"); //添加搜索效果
				getSearchItem.noSearchTap("ul.pop-tab .pop-tab-titleitem"); //点击其他遮罩层关闭搜索
				$("body").on("keyup", "#searchText", function() { //搜索框事件
					keyWordText = $("#searchText").val();
					getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
				});
			});

			//调用日期筛选传值
			require("getSearchDateItem");
			//调用状态筛选值
			require("recordStatusID");
			var startDate = ''; //开始时间
			var endDate = ''; //结束时间
			var strStatusId = []; //id筛选
			strStatusId[0] = '';
			var pageIndex = 1; //当前分页
			var dataState = true; //判断如何渲染页面，true替换加载，false加载在末尾
			var pageState = 10; //判断有多少页，调用接口再动态改
			mui.init({
				swipeBack: true, //启用右滑关闭功能
				pullRefresh: { //配置分页下拉刷新
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
			});

			//  mui('.mui-scroll-wrapper').scroll({
			//		deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
			//	});

			//按时间筛选数据
			mui('body').on('tap', '.pop-tab-con1 li', function() {
				pageIndex = 1;
				startDate = $(this).attr('data-statedate');
				endDate = $(this).attr('data-endDate');
				dataState = true;
				getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
				mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
				//		mui('#pullrefresh').pullRefresh().scrollTo(0,0,100);         //返回顶部
			});
			//按状态筛选数据
			mui('body').on('tap', '.pop-tab-con2 li', function() {
				pageIndex = 1;
				strStatusId[0] = $(this).attr('data-StatusId');
				dataState = true;
				//		mui('#pullrefresh').pullRefresh().pullupLoading();
				getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
				mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
				//		mui('#pullrefresh').pullRefresh().scrollTo(0,0,100);         //返回顶部
			});

			//初始化页面数据
			getClientList("pirchaseList", "#listPirchase", "initContentTips", true);

			mui("body").on("tap", ".outStockList", function() {
				sessionStorage.setItem('SourceId', $(this).attr("SourceId"));
				sessionStorage.setItem('AuditId', $(this).attr("AuditId"));
				sessionStorage.setItem('Audit_detailId', $(this).attr("Audit_detailId"));
				sessionStorage.setItem('SourceType', $(this).attr("SourceType"));
				sessionStorage.setItem('statusId', $(this).find(".statusId").attr("data-statusid"));
				sessionStorage.SourceId = $(this).attr("data-id");
			});

		}
	}

	//		下滑刷新具体业务实现
	function pulldownRefresh() {
		startDate = '';
		endDate = '';
		strStatusId[0] = '';
		pageIndex = 1;
		dataState = true;
		pageState = 10;
		setTimeout(function() {
			getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
			mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //刷新完成
			//			mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    //隐藏已经没数据了
			mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
		}, 1500);
	}
	//		上滑加载具体业务实现
	function pullupRefresh() {
		pageIndex++;
		dataState = false;
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh((pageState < pageIndex)); //参数为true代表没有更多数据了。
			getClientList("pirchaseList", "#listPirchase", "searchContentTips", false);
		}, 1500);
	}
	//页面初始化共用事件
	function getClientList(templateId, mainId, contentTips, isChioce) {
		//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回
		var option = {};
		var dataModel = {
			"serial": keyWordText, //(string, optional): 单号 ,
			"companyId": [], //(Array[integer], optional): 客户id eg [1,2,3] ,
			"busUserId": [], //(Array[integer], optional): 业务员 ,
			"statusId": strStatusId, //(Array[integer], optional): 状态值 ,
			"start": startDate, //(string, optional): 开始时间 ,
			"end": endDate, //(string, optional)结束时间 ,
			"orderBy": "", //(string, optional): 排序 ,
			"pageIndex": pageIndex, //(integer, optional),
			"pageSize": 20 //(integer, optional): 分页数；最大分页支持100
		};
		abp.services.app.whOutputList.getPage(dataModel, option).
		done(function(data) {
			console.log(data);
			var totalCountNum = data.totalCount; //获取数据列表条数
			pageState = Math.ceil(totalCountNum / 20); //动态修改数据列表页数
			if(totalCountNum < 1) { //动态改变无数据提示
				if(isChioce === true) {
					$('.mui-content > .tipsConInit').html("还没有货品资料哦!");
				}
				$('.mui-content').addClass(contentTips);
				$('.mui-pull-top-pocket').css('display', 'none');
			} else {
				$('.mui-content').removeClass(contentTips);
			}
			var items = data.items;
			$.each(items, function(i, v) {
				var serialDate = v.serialDate.substring(0, 10);
				var dataCreated = v.dataCreated.substring(0, 10);
				v.dataCreated = dataCreated;
				v.serialDate = serialDate;
			});
			var html = template(templateId, data);
			if(dataState === true) { //判断是页面刷新还是上滑追加数据
				$(mainId).html(html);
			} else {
				$(mainId).append(html);
			}

			var statusIdColor = require("statusIdColor");
			statusIdColor($(".statusId"));
			if(data.success) {

			} else {
				//	            		console.log(data.error.message)

			}
		}).fail(function(data) {

		});
	}

	//返回清除缓存
	var commonBack = require("commonBack");
	//不在钉钉中返回
	mui("body").on("tap", "header>button", function() {
		commonBack.secondBack();
	});
	//钉钉返回
	dingTalkNav.LeftNavBar(commonBack.secondBack);
	//ad
	dingTalkNav.leftAndroid(commonBack.secondBack);
});